<template>
  <div class="app-container">
    <bread parentPathText="资讯管理" currentPathText="资讯列表"/>
    <el-form :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="日期选择">
        <el-date-picker
          v-model="date"
           value-format="yyyy-MM-dd"
          type="daterange"
          size="mini"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="新闻类型">
        <el-select
          v-model="queryParams.newsType"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 260px"
          @change="newsTypeChange"
        >
          <el-option
            v-for="item in newsTypeData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="新闻状态">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 260px"
          @change="validChange"
        >
          <el-option
            v-for="item in validData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="addItem">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="tableList">
      <el-table-column
        align="center"
        label="新闻类型"
        prop="typeVal"
        :show-overflow-tooltip="true"
        min-width="15%"
      />
      <el-table-column
        align="center"
        label="新闻标题"
        prop="title"
        :show-overflow-tooltip="true"
        min-width="30%"
      />
       <el-table-column align="center" label="新闻状态" min-width="30%">
        <template slot-scope="scope">
          <div v-if="scope.row.status == true">有效</div>
          <div v-else>无效</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="新闻图片" min-width="30%">
        <template slot-scope="scope">
          <el-image :src="scope.row.imgPath" class="el-img"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="添加时间"
        min-width="15%"
        prop="createTime"
      />
      <el-table-column
        align="center"
        label="操作"
        min-width="25%"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="pagination"
    >
    </el-pagination>
  </div>
</template>
<script>
import initPagin from '../mixins/index.js';
export default {
  mixins:[initPagin],
  data() {
    return {
      date: "",
      queryParams: {
        newsType: "",
        status: "",
      },
      loading: false,
      newsTypeData: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "网格连心",
          value: 1,
        },
        {
          label: "党员活动",
          value: 2,
        },
      ],
      validData: [
        {
          label: "有效",
          value: 1,
        },
        {
          label: "无效",
          value: 0,
        },
      ],
      time: new Date().getTime(),
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      let params = {
        time: this.time,
        type: this.queryParams.newsType,
        startTime: this.date ? this.date[0] : "",
        endTime: this.date ? this.date[1] : "",
        status: this.queryParams.status,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.message.list, {
          time: this.time,
          type: this.queryParams.newsType,
          startTime: this.date ? this.date[0] : "",
          endTime: this.date ? this.date[1] : "",
          status: this.queryParams.status,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.tableList = res.data.data;
          this.total = res.data.count;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = "";
      });
      this.date = "";
    },
    newsTypeChange(val) {
      this.queryParams.newsType = val;
    },
    validChange(val) {
      this.queryParams.isValid = val;
    },
    // 新增
    handleAdd() {
      this.$router.push("/messageDetail");
    },
    // 编辑
    handleUpdate(row) {
      this.$router.push(`/messageDetail?id=${row.id}`)
    },
    // 删除按钮操作
    handleDelete(row) {
      let that = this;
      this.$confirm("是否确认删除?", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          that.delRole(row.id);
        })
        .catch(() => {});
    },
    // 删除
    delRole(id) {
      let params = {
        time: this.time,
        id,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.message.delete, {
          time: this.time,
          id,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="less" scoped>
.el-img {
  width: 80px;
  height: 80px;
}
</style>